/*
 * @Author: ChunLai
 * @Date: 2022-09-06 16:59:03
 * @LastEditTime: 2023-12-11 16:35:22
 * @Description: 格式化数据
 * @FilePath: \01.latest-short-insure\src\methods\format.js
 */
// 身份证号(显示)
// replace(/[^\w]/g, "").replace(/(\d{6})(\d{8})(?=[0-9a-zA-Z])/g, "$1 $2 ")
export const idNumFormat = (val, type = "c") => {
  let res = "";
  if (!val) return "";
  if (type === "s") {
    res = val
      .replace(/[^\w]/g, "")
      .replace(/(\d{6})\d*([0-9a-zA-Z]{4})/g, "$1********$2");
  } else {
    res = val
      .replace(/[^\w]/g, "")
      .replace(/(\d{6})(\d{8})(?=[0-9a-zA-Z])/g, "$1 $2 ");
  }
  return res;
};

// 手机号(显示)
export const tellPhoneFormat = (phone, type = "c") => {
  phone += "";
  let res = phone.replace(/(\d{3})(\d{4})/g, "$1 $2 ");
  if (type === "s") {
    res = phone.replace(/(\d{3})\d*(\d{4})/g, "$1***$2");
  }
  // phone.substring(0, 3) + '****' + phone.substring(7);
  return res;
};

// 银行账号(显示)
export const accountCodeFormat = (value) => {
  if (value) {
    return value.replace(/(\d{4})(?=\d)/g, "$1 ");
  } else {
    return "";
  }
};

export const rebackUrlPath = (pname = "") => {
  let res = "";
  const winLoc = `${window.location.origin}${window.location.pathname}`,
    winHash = window.location.hash.split("?");
  let hash_val = `${winHash[0]}`;
  if (pname) {
    hash_val = `${pname}`;
  }
  res = `${winLoc}${window.location.search}${hash_val}`;
  return res;
};

export const filterRebackPath = (filterArr = []) => {
  let res = "";
  const winLoc = `${window.location.origin}${window.location.pathname}`,
    prmsArr = window.location.search.split("&");
  let hash_val = "";
  if (window.location.hash) {
    const hashArr = window.location.hash.split("?");
    hash_val = `${hashArr[0]}`;
  }
  let resPams = prmsArr.filter((item) => {
    const arr1 = item.split("=");
    if (!filterArr.includes(arr1[0])) {
      return true;
    }
  });
  res = `${winLoc}${resPams.join("&")}${hash_val}`;
  return res;
};
